
package net.blogways.ordermgr.order;

import com.asiainfo.bpc.exception.BusiException;
import com.asiainfo.bpc.service.SqlCodeService;
import com.asiainfo.bpc.utils.ToolFactory;
import com.asiainfo.utils.Utils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.blogways.ordermgr.common.service.CommonService;
import net.blogways.ordermgr.mail.service.MailService;
import net.blogways.ordermgr.util.Constants;
import net.blogways.ordermgr.util.DBTools;
import net.blogways.ordermgr.util.ErrorCode;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.*;

@Component("order:orderMgr")
public class OrderService implements Command {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public boolean execute(Context context) throws Exception {
        return false;
    }

    /**
     * 获取待审核、待生产订单数
     * @param context
     * @return
     * @throws Exception
     */
    public Boolean getOrdersState(Context context)throws Exception{
        SqlCodeService sqlCodeService = ToolFactory.getInstance().getSqlCodeService();
        String userName = context.get("USER_NAME").toString();
        String userType = context.get("USER_TYPE").toString();
        Map sqlParams = new HashMap();
        switch (userType){
            case "-1":
                List productings = sqlCodeService.select("SEL_ORDER_PRODUCTTING", "TF_B_ORDER", new HashMap<>());
                Map product = (Map) productings.get(0);
                long productingNum = (long) product.get("PRODUCTING_NUM");

                List auditings = sqlCodeService.select("SEL_ORDER_AUDITING", "TF_B_ORDER", new HashMap<>());
                Map audit = (Map) auditings.get(0);
                long auditNum = (long) audit.get("AUDITING_NUM");

                List unpasses = sqlCodeService.select("SEL_ORDER_UNPASS", "TF_B_ORDER", new HashMap<>());
                Map unpass = (Map) unpasses.get(0);
                long unpassNum = (long) unpass.get("UNPASS_NUM");

                context.put("productingNum", productingNum);
                context.put("auditNum", auditNum);
                context.put("unpassNum", unpassNum);
                break;
            case "0":
                sqlParams.put("DIRECTOR", userName);
                List productings1 = sqlCodeService.select("SEL_ORDER_PRODUCTTING_DIRECTOR", "TF_B_ORDER", sqlParams);
                Map product1 = (Map) productings1.get(0);
                long productingNum1 = (long) product1.get("PRODUCTING_NUM");

                List auditings1 = sqlCodeService.select("SEL_ORDER_AUDITING_DIRECTOR", "TF_B_ORDER", sqlParams);
                Map audit1 = (Map) auditings1.get(0);
                long auditNum1 = (long) audit1.get("AUDITING_NUM");

                List unpasses1 = sqlCodeService.select("SEL_ORDER_UNPASS_DIRECTOR", "TF_B_ORDER", sqlParams);
                Map unpass1 = (Map) unpasses1.get(0);
                long unpassNum1 = (long) unpass1.get("UNPASS_NUM");

                context.put("productingNum", productingNum1);
                context.put("auditNum", auditNum1);
                context.put("unpassNum", unpassNum1);
                break;
            case "1":
                sqlParams.put("CREATE_STAFF_ID", userName);
                List productings2 = sqlCodeService.select("SEL_ORDER_PRODUCTTING_SALE", "TF_B_ORDER", sqlParams);
                Map product2 = (Map) productings2.get(0);
                long productingNum2 = (long) product2.get("PRODUCTING_NUM");

                List auditings2 = sqlCodeService.select("SEL_ORDER_AUDITING_SALE", "TF_B_ORDER", sqlParams);
                Map audit2 = (Map) auditings2.get(0);
                long auditNum2 = (long) audit2.get("AUDITING_NUM");

                List unpasses2 = sqlCodeService.select("SEL_ORDER_UNPASS_SALE", "TF_B_ORDER", sqlParams);
                Map unpass2 = (Map) unpasses2.get(0);
                long unpassNum2 = (long) unpass2.get("UNPASS_NUM");

                context.put("productingNum", productingNum2);
                context.put("auditNum", auditNum2);
                context.put("unpassNum", unpassNum2);
                break;
            case "2":
                sqlParams.put("MERCHANDISER", userName);
                List productings3 = sqlCodeService.select("SEL_ORDER_PRODUCTTING_TRACK", "TF_B_ORDER", sqlParams);
                Map product3 = (Map) productings3.get(0);
                long productingNum3 = (long) product3.get("PRODUCTING_NUM");

                List auditings3 = sqlCodeService.select("SEL_ORDER_AUDITING_TRACK", "TF_B_ORDER", sqlParams);
                Map audit3 = (Map) auditings3.get(0);
                long auditNum3 = (long) audit3.get("AUDITING_NUM");

                List unpasses3 = sqlCodeService.select("SEL_ORDER_UNPASS_TRACK", "TF_B_ORDER", sqlParams);
                Map unpass3 = (Map) unpasses3.get(0);
                long unpassNum3 = (long) unpass3.get("UNPASS_NUM");

                context.put("productingNum", productingNum3);
                context.put("auditNum", auditNum3);
                context.put("unpassNum", unpassNum3);
        }

        return Utils.returnOkResult(context, "查询成功！");
    }

    /**
     * 删除订单
     * @param context
     * @return
     * @throws Exception
     */
    public Boolean deleteOrder(Context context)throws Exception{
        List orders = (List) context.get("orders");
        List orderIds = new ArrayList();
        for (int i = 0; i < orders.size(); i++) {
            Map order = (Map) orders.get(i);
            orderIds.add(order.get("ORDER_ID"));
        }

        Map sqlParams = new HashMap();
        SqlCodeService sqlCodeService = ToolFactory.getInstance().getSqlCodeService();
        sqlParams.put("ORDER_IDS", orderIds);
        orders = sqlCodeService.select("SEL_SQL_ORDER_BY_ID", "TF_B_ORDER", sqlParams);

        List waitDel = new ArrayList();
        for (int i = 0; i < orders.size(); i++) {
            Map order = (Map) orders.get(i);
            String orderState = (String) order.get("STATE");
            if (Constants.ORDER_STATE_AUDITING.equals(orderState)
                    || Constants.ORDER_STATE_DRAFT.equals(orderState)
                    || Constants.ORDER_STATE_UNAUDITED.equals(orderState)
                    || Constants.ORDER_STATE_TOBE_PROD.equals(orderState)){
                waitDel.add(order.get("ORDER_ID"));
            }
        }

        if (waitDel.size() == 0){
            return Utils.returnOkResult(context, "删除订单成功！");
        }

        sqlParams.put("ORDER_IDS", waitDel);
        sqlCodeService.update("UPD_ORDER_DEL", "TF_B_ORDER", sqlParams);
        sqlCodeService.update("UPD_ORDER_DEL", "TF_B_ORDER_CUSTOM", sqlParams);
        sqlCodeService.update("UPD_ORDER_DEL", "TF_B_ORDER_DRY", sqlParams);
        sqlCodeService.update("UPD_ORDER_DEL", "TF_B_ORDER_BOTTLEDWASH", sqlParams);
        sqlCodeService.update("UPD_ORDER_DEL", "TF_B_TAB_ORDER", sqlParams);
        sqlCodeService.update("UPD_ORDER_DEL", "TF_B_ORDER_SOAP", sqlParams);
        sqlCodeService.update("UPD_ORDER_DEL", "TF_B_ORDER_SLIPPER", sqlParams);
        sqlCodeService.update("UPD_ORDER_DEL", "TF_B_ORDER_HOSEWASH", sqlParams);
        sqlCodeService.update("UPD_ORDER_DEL", "tf_b_order_attach", sqlParams);

        sqlCodeService.delete("UPD_ORDER_DEL", "TF_B_ORDER_LOG", sqlParams);

        return Utils.returnOkResult(context, "删除订单成功！");
    }

    /**
     * 订单保存前置.
     *
     * @param context
     * @return
     */
    public Boolean saveOrderBefore(Context context) throws Exception{

        logger.debug("*************** saveOrderBefore begin, context = {} ***************", context);

        Map base = (Map) context.get("Base");
        Map json = (Map) context.get("JSON");

        String userName = base.get("USER_NAME").toString();
        if (json.get("TF_B_ORDER") != null && ((List) json.get("TF_B_ORDER")).size() > 0) {
            List orderList = (List) json.get("TF_B_ORDER");
            Map orderM = (Map) orderList.get(0);
            orderM.put("CREATE_STAFF_ID", userName);
            orderM.put("UPDATE_STAFF_ID", userName);

            Map param = Maps.newHashMap();
            param.put("ORDER_ID", orderM.get("ORDER_ID"));

            List list = ToolFactory.getInstance().getSqlCodeService()
                    .select("SEL_ORDER_BY_ID", "TF_B_ORDER", param);

            if (list.size() > 0) {
                logger.debug("*************** saveOrderBefore end ***************", context);
                logger.error("ORDER_ID = {} has already exist in TF_B_ORDER", orderM.get("ORDER_ID"));
                return Utils.returnFalseResult(context, ErrorCode.ORDER_ID_EXIST, "订单编号已存在请重新输入");
            }
        }

        if (json.get("TF_B_ORDER_SLIPPER") != null && ((List) json.get("TF_B_ORDER_SLIPPER")).size() > 0) {
            List<Map<String, Object>> slipperList = (List) json.get("TF_B_ORDER_SLIPPER");
            for (int i = 0; i < slipperList.size(); i++) {
                Map slipperTmp = slipperList.get(i);
                if (!StringUtils.isEmpty(slipperTmp.get("UNIT_PRICE"))) {
                    if (!DBTools.checkPrice(slipperTmp.get("UNIT_PRICE").toString())) {
                        logger.debug("*************** saveOrderBefore end ***************", context);
                        logger.error("price illage, param = {}", slipperTmp);
                        return Utils.returnFalseResult(context, ErrorCode.ORDER_ILLAGE_PRICE,
                                "拖鞋" + ErrorCode.ORDER_ILLAGE_PRICE_MSG);
                    }
                }

                if (!StringUtils.isEmpty(slipperTmp.get("AMOUNT"))) {
                    if (!DBTools.checkQuantity(slipperTmp.get("AMOUNT").toString())) {
                        logger.debug("*************** saveOrderBefore end ***************", context);
                        logger.error("AMOUNT illage, param = {}", slipperTmp);
                        return Utils.returnFalseResult(context, ErrorCode.ORDER_ILLAGE_AMOUNT,
                                "拖鞋" + ErrorCode.ORDER_ILLAGE_AMOUNT_MSG);

                    }
                }
            }
        }

        if (json.get("TF_B_ORDER_HOSEWASH") != null && ((List) json.get("TF_B_ORDER_HOSEWASH")).size() > 0) {
            List<Map<String, Object>> hoseWashrList = (List) json.get("TF_B_ORDER_HOSEWASH");
            for (int i = 0; i < hoseWashrList.size(); i++) {
                Map hoseTmp = hoseWashrList.get(i);
                if (!StringUtils.isEmpty(hoseTmp.get("UNIT_PRICE"))) {
                    if (!DBTools.checkPrice(hoseTmp.get("UNIT_PRICE").toString())) {
                        logger.debug("*************** saveOrderBefore end ***************", context);
                        logger.error("price illage, param = {}", hoseTmp);
                        return Utils.returnFalseResult(context, ErrorCode.ORDER_ILLAGE_PRICE,
                                "软管洗护" + ErrorCode.ORDER_ILLAGE_PRICE_MSG);
                    }
                }

                if (!StringUtils.isEmpty(hoseTmp.get("HEIGHT"))) {
                    if (!DBTools.checkPrice(hoseTmp.get("HEIGHT").toString())) {
                        logger.debug("*************** saveOrderBefore end ***************", context);
                        logger.error("HEIGHT illage, param = {}", hoseTmp);
                        return Utils.returnFalseResult(context, ErrorCode.ORDER_ILLAGE_HEIGHT,
                                "软管洗护" + ErrorCode.ORDER_ILLAGE_HEIGHT_MSG);

                    }
                }

                if (!StringUtils.isEmpty(hoseTmp.get("AMOUNT"))) {
                    if (!DBTools.checkQuantity(hoseTmp.get("AMOUNT").toString())) {
                        logger.debug("*************** saveOrderBefore end ***************", context);
                        logger.error("AMOUNT illage, param = {}", hoseTmp);
                        return Utils.returnFalseResult(context, ErrorCode.ORDER_ILLAGE_AMOUNT,
                                "软管洗护" + ErrorCode.ORDER_ILLAGE_AMOUNT_MSG);

                    }
                }

                if (!StringUtils.isEmpty(hoseTmp.get("LIQUID_QUALITY"))) {
                    if (!DBTools.checkPrice(hoseTmp.get("LIQUID_QUALITY").toString())) {
                        logger.debug("*************** saveOrderBefore end ***************", context);
                        logger.error("LIQUID_QUALITY illage, param = {}", hoseTmp);
                        return Utils.returnFalseResult(context, ErrorCode.ORDER_ILLAGE_LIQUID_QUALITY,
                                "软管洗护" + ErrorCode.ORDER_ILLAGE_LIQUID_QUALITY_MSG);
                    }
                }


                if (!StringUtils.isEmpty(hoseTmp.get("LIQUID_WEIGHT"))) {
                    if (!DBTools.checkPrice(hoseTmp.get("LIQUID_WEIGHT").toString())) {
                        logger.debug("*************** saveOrderBefore end ***************", context);
                        logger.error("LIQUID_WEIGHT illage, param = {}", hoseTmp);
                        return Utils.returnFalseResult(context, ErrorCode.ORDER_ILLAGE_LIQUID_WEIGHT,
                                "软管洗护" + ErrorCode.ORDER_ILLAGE_LIQUID_WEIGHT_MSG);

                    }
                }

                if (!StringUtils.isEmpty(hoseTmp.get("LIQUID_PACKING_PRICE"))) {
                    if (!DBTools.checkPrice(hoseTmp.get("LIQUID_PACKING_PRICE").toString())) {
                        logger.debug("*************** saveOrderBefore end ***************", context);
                        logger.error("LIQUID_PACKING_PRICE illage, param = {}", hoseTmp);
                        return Utils.returnFalseResult(context, ErrorCode.ORDER_ILLAGE_LIQUID_PACKING_PRICE,
                                "软管洗护" + ErrorCode.ORDER_ILLAGE_LIQUID_PACKING_PRICE_MSG);

                    }
                }


            }
        }

        if (json.get("TF_B_ORDER_BOTTLEDWASH") != null && ((List) json.get("TF_B_ORDER_BOTTLEDWASH")).size() > 0) {
            List<Map<String, Object>> bottleWashrList = (List) json.get("TF_B_ORDER_BOTTLEDWASH");
            for (int i = 0; i < bottleWashrList.size(); i++) {
                Map bootleTmp = bottleWashrList.get(i);
                if (!StringUtils.isEmpty(bootleTmp.get("UNIT_PRICE"))) {
                    if (!DBTools.checkPrice(bootleTmp.get("UNIT_PRICE").toString())) {
                        logger.debug("*************** saveOrderBefore end ***************", context);
                        logger.error("price illage, param = {}", bootleTmp);
                        return Utils.returnFalseResult(context, ErrorCode.ORDER_ILLAGE_PRICE,
                                "瓶装洗护" + ErrorCode.ORDER_ILLAGE_PRICE_MSG);
                    }
                }


                if (!StringUtils.isEmpty(bootleTmp.get("AMOUNT"))) {
                    if (!DBTools.checkQuantity(bootleTmp.get("AMOUNT").toString())) {
                        logger.debug("*************** saveOrderBefore end ***************", context);
                        logger.error("AMOUNT illage, param = {}", bootleTmp);
                        return Utils.returnFalseResult(context, ErrorCode.ORDER_ILLAGE_AMOUNT,
                                "瓶装洗护" + ErrorCode.ORDER_ILLAGE_AMOUNT_MSG);

                    }
                }

                if (!StringUtils.isEmpty(bootleTmp.get("LIQUID_QUALITY"))) {
                    if (!DBTools.checkPrice(bootleTmp.get("LIQUID_QUALITY").toString())) {
                        logger.debug("*************** saveOrderBefore end ***************", context);
                        logger.error("LIQUID_QUALITY illage, param = {}", bootleTmp);
                        return Utils.returnFalseResult(context, ErrorCode.ORDER_ILLAGE_LIQUID_QUALITY,
                                "瓶装洗护" + ErrorCode.ORDER_ILLAGE_LIQUID_QUALITY_MSG);
                    }
                }


                if (!StringUtils.isEmpty(bootleTmp.get("LIQUID_WEIGHT"))) {
                    if (!DBTools.checkPrice(bootleTmp.get("LIQUID_WEIGHT").toString())) {
                        logger.debug("*************** saveOrderBefore end ***************", context);
                        logger.error("LIQUID_WEIGHT illage, param = {}", bootleTmp);
                        return Utils.returnFalseResult(context, ErrorCode.ORDER_ILLAGE_LIQUID_WEIGHT,
                                "瓶装洗护" + ErrorCode.ORDER_ILLAGE_LIQUID_WEIGHT_MSG);

                    }
                }

                if (!StringUtils.isEmpty(bootleTmp.get("LIQUID_PACKING_PRICE"))) {
                    if (!DBTools.checkPrice(bootleTmp.get("LIQUID_PACKING_PRICE").toString())) {
                        logger.debug("*************** saveOrderBefore end ***************", context);
                        logger.error("LIQUID_PACKING_PRICE illage, param = {}", bootleTmp);
                        return Utils.returnFalseResult(context, ErrorCode.ORDER_ILLAGE_LIQUID_PACKING_PRICE,
                                "瓶装洗护" + ErrorCode.ORDER_ILLAGE_LIQUID_PACKING_PRICE_MSG);

                    }
                }


            }
        }

        if (json.get("TF_B_ORDER_SOAP") != null && ((List) json.get("TF_B_ORDER_SOAP")).size() > 0) {
            List<Map<String, Object>> soapList = (List) json.get("TF_B_ORDER_SOAP");
            for (int i = 0; i < soapList.size(); i++) {
                Map soapTmp = soapList.get(i);
                if (!StringUtils.isEmpty(soapTmp.get("UNIT_PRICE"))) {
                    if (!DBTools.checkPrice(soapTmp.get("UNIT_PRICE").toString())) {
                        logger.debug("*************** saveOrderBefore end ***************", context);
                        logger.error("UNIT_PRICE illage, param = {}", soapTmp);
                        return Utils.returnFalseResult(context, ErrorCode.ORDER_ILLAGE_PRICE,
                                "香皂" + ErrorCode.ORDER_ILLAGE_PRICE_MSG);
                    }
                }

                if (!StringUtils.isEmpty(soapTmp.get("QUALITY"))) {
                    if (!DBTools.checkPrice(soapTmp.get("QUALITY").toString())) {
                        logger.debug("*************** saveOrderBefore end ***************", context);
                        logger.error("QUALITY illage, param = {}", soapTmp);
                        return Utils.returnFalseResult(context, ErrorCode.ORDER_ILLAGE_QUALITY,
                                "香皂" + ErrorCode.ORDER_ILLAGE_QUALITY_MSG);
                    }
                }

                if (!StringUtils.isEmpty(soapTmp.get("PACKING_PRICE"))) {
                    if (!DBTools.checkPrice(soapTmp.get("PACKING_PRICE").toString())) {
                        logger.debug("*************** saveOrderBefore end ***************", context);
                        logger.error("PACKING_PRICE illage, param = {}", soapTmp);
                        return Utils.returnFalseResult(context, ErrorCode.ORDER_ILLAGE_PACKING_PRICE,
                                "香皂" + ErrorCode.ORDER_ILLAGE_PACKING_PRICE_MSG);

                    }
                }

                if (!StringUtils.isEmpty(soapTmp.get("AMOUNT"))) {
                    if (!DBTools.checkQuantity(soapTmp.get("AMOUNT").toString())) {
                        logger.debug("*************** saveOrderBefore end ***************", context);
                        logger.error("AMOUNT illage, param = {}", soapTmp);
                        return Utils.returnFalseResult(context, ErrorCode.ORDER_ILLAGE_AMOUNT,
                                "香皂" + ErrorCode.ORDER_ILLAGE_AMOUNT_MSG);

                    }
                }

            }
        }

        if (json.get("TF_B_ORDER_DRY") != null && ((List) json.get("TF_B_ORDER_DRY")).size() > 0) {
            List<Map<String, Object>> dryList = (List) json.get("TF_B_ORDER_DRY");
            for (int i = 0; i < dryList.size(); i++) {
                Map dryTmp = dryList.get(i);
                if (!StringUtils.isEmpty(dryTmp.get("IN_PACKING_PRICE"))) {
                    if (!DBTools.checkPrice(dryTmp.get("IN_PACKING_PRICE").toString())) {
                        logger.debug("*************** saveOrderBefore end ***************", context);
                        logger.error("IN_PACKING_PRICE illage, param = {}", dryTmp);
                        return Utils.returnFalseResult(context, ErrorCode.ORDER_ILLAGE_IN_PACKING_PRICE,
                                "包装干货" + ErrorCode.ORDER_ILLAGE_IN_PACKING_PRICE_MSG);
                    }
                }

                if (!StringUtils.isEmpty(dryTmp.get("OUT_PACKING_PRICE"))) {
                    if (!DBTools.checkPrice(dryTmp.get("OUT_PACKING_PRICE").toString())) {
                        logger.debug("*************** saveOrderBefore end ***************", context);
                        logger.error("OUT_PACKING_PRICE illage, param = {}", dryTmp);
                        return Utils.returnFalseResult(context, ErrorCode.ORDER_ILLAGE_OUT_PACKING_PRICE,
                                "包装干货" + ErrorCode.ORDER_ILLAGE_OUT_PACKING_PRICE_MSG);
                    }
                }

                if (!StringUtils.isEmpty(dryTmp.get("PACKING_TIME"))) {
                    if (!DBTools.checkPrice(dryTmp.get("PACKING_TIME").toString())) {
                        logger.debug("*************** saveOrderBefore end ***************", context);
                        logger.error("PACKING_TIME illage, param = {}", dryTmp);
                        return Utils.returnFalseResult(context, ErrorCode.ORDER_ILLAGE_PACKING_TIME,
                                "包装干货" + ErrorCode.ORDER_ILLAGE_PACKING_TIME_MSG);

                    }
                }

                if (!StringUtils.isEmpty(dryTmp.get("AMOUNT"))) {
                    if (!DBTools.checkQuantity(dryTmp.get("AMOUNT").toString())) {
                        logger.debug("*************** saveOrderBefore end ***************", context);
                        logger.error("AMOUNT illage, param = {}", dryTmp);
                        return Utils.returnFalseResult(context, ErrorCode.ORDER_ILLAGE_AMOUNT,
                                "包装干货" + ErrorCode.ORDER_ILLAGE_AMOUNT_MSG);

                    }
                }

            }
        }

        if (json.get("TF_B_ORDER_CUSTOM") != null && ((List) json.get("TF_B_ORDER_CUSTOM")).size() > 0) {
            List<Map<String, Object>> customList = (List) json.get("TF_B_ORDER_CUSTOM");
            for (int i = 0; i < customList.size(); i++) {
                Map customTmp = customList.get(i);
                if (!StringUtils.isEmpty(customTmp.get("UNIT_PRICE"))) {
                    if (!DBTools.checkPrice(customTmp.get("UNIT_PRICE").toString())) {
                        logger.debug("*************** saveOrderBefore end ***************", context);
                        logger.error("price illage, param = {}", customTmp);
                        return Utils.returnFalseResult(context, ErrorCode.ORDER_ILLAGE_PRICE,
                                "自定义" + ErrorCode.ORDER_ILLAGE_PRICE_MSG);
                    }
                }

                if (!StringUtils.isEmpty(customTmp.get("AMOUNT"))) {
                    if (!DBTools.checkQuantity(customTmp.get("AMOUNT").toString())) {
                        logger.debug("*************** saveOrderBefore end ***************", context);
                        logger.error("AMOUNT illage, param = {}", customTmp);
                        return Utils.returnFalseResult(context, ErrorCode.ORDER_ILLAGE_AMOUNT,
                                "自定义" + ErrorCode.ORDER_ILLAGE_AMOUNT_MSG);

                    }
                }
            }
        }



        List logList = (List) json.get("TF_B_ORDER_LOG");
        Map logM = (Map) logList.get(0);
        if (logM.get("MODIFY_TAG").toString().equals("0")){
            logM.put("OPERATE_STAFF_ID", userName);
        }

        logger.debug("*************** saveOrderBefore end, context = {} ***************", context);
        return Utils.returnOkResult(context, "操作完成");
    }

  /**
   * 根据订单编号查询订单
   *
   * @param context
   * @return
   * @throws Exception
   */
  public Boolean getOrderByOrderId(Context context) throws Exception {
    String orderId = (String) context.get("orderId");
    SqlCodeService sqlCodeService = ToolFactory.getInstance().getSqlCodeService();
    Map<String, Object> params = Maps.newHashMap();
    params.put("ORDER_IDS", Lists.newArrayList(orderId));
    List orders = sqlCodeService.select("SEL_SQL_ORDER_BY_ID", "TF_B_ORDER", params);
    Assert.notEmpty(orders, "无对应订单");
    Assert.isTrue(orders.size() == 1, "订单不唯一");
    return Utils.returnOk(context, orders);
  }
    /**
     * 订单详情查询.
     * @param context
     * @return
     * @throws Exception
     */
    public boolean queryOrderDetail(Context context) throws Exception{

        logger.debug("*********** queryOrderDetail begin, context = {} ***********", context);

        String orderId = context.get("ORDER_ID").toString();

        Map map = Maps.newHashMap();
        map.put("ORDER_ID", orderId);

        List orderList = ToolFactory.getInstance().getSqlCodeService()
                .select("SEL_ORDER_DETAIL", "TF_B_ORDER", map);

        if (orderList.size() < 1){
            logger.error("query order detail fail, no such order exist. map = {}", map);
            logger.debug("*********** queryOrderDetail end, context = {} ***********", context);
            return Utils.returnFalseResult(context, ErrorCode.ORDER_NOT_EXIST, "不存在该订单");
        }

        Map orderM = (Map) orderList.get(0);

        CommonService commonService = (CommonService) ToolFactory.getInstance().getContext()
                .getBean("commonService");

        orderM.put("STATUS_NAME", commonService.
                dictCode2Name("order_state", orderM.get("STATE").toString(), "zh"));

        Map resultMap = Maps.newHashMap();
        resultMap.put("ORDER_INFO", orderList);

        map.remove("CREATE_STAFF_ID");

        //拖鞋
        List slipperList = ToolFactory.getInstance().getSqlCodeService()
                .select("SEL_SLIPPER_DETAIL", "TF_B_ORDER_SLIPPER", map);
        if (slipperList.size() > 0){
            resultMap.put("SLIPPER_INFO", slipperList);
        }

        //软管洗护
        List hoseWashList = ToolFactory.getInstance().getSqlCodeService()
                .select("SEL_HOSEWASH_DETAIL", "TF_B_ORDER_HOSEWASH", map);
        if (hoseWashList.size() > 0){
            resultMap.put("HOSEWASH_INFO", hoseWashList);
            for (int i = 0; i < hoseWashList.size(); i++){
                Map m = (Map) hoseWashList.get(i);
                if (!StringUtils.isEmpty(m.get("COVER_CLOSS"))){
                    m.put("COVER_CLOSS_NAME", StringUtils.isEmpty(m.get("COVER_CLOSS")) ? "" : commonService.
                            dictCode2Name("coverGloss", m.get("COVER_CLOSS").toString(), "zh"));
                }
                if (!StringUtils.isEmpty(m.get("LIQUID_TYPE"))){
                    Map r = commonService.getTypeNameByCode("td_s_liquid_type",
                            "TYPE_CODE:"+m.get("LIQUID_TYPE"),"TYPE_NAME", "", "");
                    m.put("LIQUID_TYPE_NAME", r.get("TYPE_NAME"));
                } else {
                    m.put("LIQUID_TYPE_NAME", "");
                }
            }
        }

        //瓶装洗护
        List bottledWashList = ToolFactory.getInstance().getSqlCodeService()
                .select("SEL_BOTTLEDWASH_DETAIL", "TF_B_ORDER_BOTTLEDWASH", map);
        if (bottledWashList.size() > 0){
            resultMap.put("BOTTLEDWASH_INFO", bottledWashList);
            for (int i = 0; i < bottledWashList.size(); i++){
                Map bottledWashM = (Map) bottledWashList.get(i);

                if (!StringUtils.isEmpty(bottledWashM.get("LIQUID_TYPE"))){
                    Map r = commonService.getTypeNameByCode("td_s_liquid_type",
                            "TYPE_CODE:"+bottledWashM.get("LIQUID_TYPE"),"TYPE_NAME", "", "");
                    bottledWashM.put("LIQUID_TYPE_NAME", r.get("TYPE_NAME"));
                } else {
                    bottledWashM.put("LIQUID_TYPE_NAME", "");
                }

                if (!StringUtils.isEmpty(bottledWashM.get("COVER_CLOSS"))){
                    bottledWashM.put("COVER_CLOSS_NAME", StringUtils.isEmpty(bottledWashM.get("COVER_CLOSS")) ? "" : commonService.
                            dictCode2Name("coverGloss", bottledWashM.get("COVER_CLOSS").toString(), "zh"));
                }
                if (!StringUtils.isEmpty(bottledWashM.get("PRINTER"))){
                    bottledWashM.put("PRINTER_NAME", StringUtils.isEmpty(bottledWashM.get("PRINTER")) ? "" :commonService.
                            dictCode2Name("print", bottledWashM.get("PRINTER").toString(), "zh"));
                }
                if (!StringUtils.isEmpty(bottledWashM.get("MAT"))){
                    bottledWashM.put("MAT_NAME", StringUtils.isEmpty(bottledWashM.get("MAT")) ? "" : commonService.
                            dictCode2Name("noneOrCustomize", bottledWashM.get("MAT").toString(), "zh"));
                }
            }
        }

        //香皂
        List soapList = ToolFactory.getInstance().getSqlCodeService()
                .select("SEL_SOAP_DETAIL", "TF_B_ORDER_SOAP", map);
        if (soapList.size() > 0){
            resultMap.put("SOAP_INFO", soapList);
            for (int j = 0; j < soapList.size(); j++){
                Map soapM = (Map) soapList.get(j);
                soapM.put("GULE_INFO_NAME", StringUtils.isEmpty(soapM.get("GULE_INFO")) ? "" : commonService.
                        dictCode2Name("noneOrCustomize", soapM.get("GULE_INFO").toString(), "zh"));
                if (!StringUtils.isEmpty(soapM.get("GULE_INFO")) && soapM.get("GULE_INFO").toString().equals("1")) {
                    soapM.put("EASY_TEAR_NAME", StringUtils.isEmpty(soapM.get("EASY_TEAR")) ? "" :
                            soapM.get("EASY_TEAR").toString().equals("0") ? "无" : "有");
                }
            }
        }

        //包装干货
        List dryList = ToolFactory.getInstance().getSqlCodeService()
                .select("SEL_DRY_DETAIL", "TF_B_ORDER_DRY", map);
        if (dryList.size() > 0){
            resultMap.put("DRY_INFO", dryList);
        }

        //自定义
        List customList = ToolFactory.getInstance().getSqlCodeService()
                .select("SEL_CUSTOM_DETAIL", "TF_B_ORDER_CUSTOM", map);
        if (customList.size() > 0){
            resultMap.put("CUSTOM_INFO", customList);
        }

        List dmList = getDesAndMark(map);

        if (dmList.size() > 0){
            resultMap.put("DES_MARK", dmList);
        }

        //table顺序
        List tabOrderList = ToolFactory.getInstance().getSqlCodeService()
                .select("SEL_TAB_ORDER", "TF_B_TAB_ORDER", map);
        if (tabOrderList.size() > 0){
            resultMap.put("TAB_ORDER_INFO", tabOrderList);
        }


        logger.debug("*********** queryOrderDetail end, resultMap = {} ***********", resultMap);

        return Utils.returnOk(context, resultMap);
    }


  /**
   * 查询订单明细
   * @param context
   * @return
   * @throws Exception
   */
  public Boolean getOrderDetailsByOderId(Context context) throws Exception{
      String orderId = (String) context.get("orderId");
      LinkedHashMap<String,List<Map<String,Object>>> orderDetails = Maps.newLinkedHashMap();
      SqlCodeService sqlCodeService = ToolFactory.getInstance().getSqlCodeService();
      Map<String,String> params = Maps.newHashMap();
      params.put("ORDER_ID",orderId);
      List<Map<String,Object>> orderSlippers = sqlCodeService.select("SEL_SLIPPER_DETAIL","TF_B_ORDER_SLIPPER",params);
      if(CollectionUtils.isNotEmpty(orderSlippers)){
        orderDetails.put(Constants.ORDER_SLIPPER,orderSlippers);
      }
      List<Map<String,Object>> orderSoaps = sqlCodeService.select("SEL_SOAP_DETAIL","TF_B_ORDER_SOAP",params);
      if(CollectionUtils.isNotEmpty(orderSoaps)){
        orderDetails.put(Constants.ORDER_SOAP,orderSoaps);
      }
      List<Map<String,Object>> orderHoses = sqlCodeService.select("SEL_HOSEWASH_DETAIL","TF_B_ORDER_HOSEWASH",params);
      if(CollectionUtils.isNotEmpty(orderHoses)){
        orderDetails.put(Constants.ORDER_HOSEWASH,orderHoses);
      }
      List<Map<String,Object>> orderBottled = sqlCodeService.select("SEL_BOTTLEDWASH_DETAIL","TF_B_ORDER_BOTTLEDWASH",params);
      if(CollectionUtils.isNotEmpty(orderBottled)){
        orderDetails.put(Constants.ORDER_BOTTLEDWASH,orderBottled);
      }
      List<Map<String,Object>> orderDries = sqlCodeService.select("SEL_DRY_DETAIL","TF_B_ORDER_DRY",params);
      if(CollectionUtils.isNotEmpty(orderDries)){
        orderDetails.put(Constants.ORDER_DRY,orderDries);
      }
      List<Map<String,Object>> orderCustoms = sqlCodeService.select("SEL_CUSTOM_DETAIL","TF_B_ORDER_CUSTOM",params);
      if(CollectionUtils.isNotEmpty(orderCustoms)){
        orderDetails.put(Constants.ORDER_CUSTOM,orderCustoms);
      }
      if(orderDetails.size() > 0){
        context.put("orderDetails",orderDetails);
      }
      return Utils.returnOkResult(context,"查询成功");
  }



    /**
     * 审核订单
     * @param context
     * @return
     * @throws Exception
     */
    public Boolean auditOrder(Context context)throws Exception{
        List orders = (List) context.get("orders");
        Map result = (Map) context.get("result");
        String userName = context.get("userName").toString();

        String adopt = result.get("adopt").toString();
        String state;

        switch (adopt){
            case "1":
                state = "3";
                break;
            case "0":
                state = "2";
                break;
            default:
                state = "1";
                break;
        }

        List orderIds = new ArrayList();
        for (int i = 0; i < orders.size(); i++) {
            Map order = (Map) orders.get(i);
            orderIds.add(order.get("ORDER_ID"));
        }

        Map sqlParams = new HashMap();
        SqlCodeService sqlCodeService = ToolFactory.getInstance().getSqlCodeService();
        sqlParams.put("ORDER_IDS", orderIds);
        orders = sqlCodeService.select("SEL_SQL_ORDER_BY_ID", "TF_B_ORDER", sqlParams);

        List waitAudit = new ArrayList();
        Map logParams = new HashMap();
        StringBuffer intLogSQL = new StringBuffer("INSERT INTO \n" +
                "TF_B_ORDER_LOG\n" +
                "(\n" +
                "\tORDER_ID, OPERATE_CODE, OPERATE_STAFF_ID, OPERATE_TIME,START_DATE, END_DATE, REMARK\n" +
                ")\n" +
                "VALUES ");

        List needSendEmail = new ArrayList();
        //组织参数
        for (int i = 0; i < orders.size(); i++) {
            Map order = (Map) orders.get(i);
            String orderState = (String) order.get("STATE");
            if (Constants.ORDER_STATE_AUDITING.equals(orderState)){
                waitAudit.add(order.get("ORDER_ID"));
                intLogSQL.append("(:ORDER_ID")
                        .append(i)
                        .append(", :OPERATE_CODE")
                        .append(", :OPERATE_STAFF_ID")
                        .append(", NOW()")
                        .append(", NOW()")
                        .append(", '2099-12-31 23:59:59'")
                        .append(", :REMARK),");
                logParams.put("ORDER_ID" + i, order.get("ORDER_ID"));

                //审核通过，需要发邮件
                if (adopt.equals("1")){
                    Map mailParam = new HashMap();
                    mailParam.put("templateName", "notice.ftl");

                    Map mailInfo = new HashMap();
                    mailInfo.put("USER_NAME", order.get("MERCHANDISER"));
                    mailInfo.put("ORDER_ID", order.get("ORDER_ID"));
                    mailInfo.put("DELIVER_TIME", order.get("DELIVER_TIME"));
                    mailParam.put("mailInfo", mailInfo);

                    Map getUserParams = new HashMap();
                    getUserParams.put("USER_NAME", order.get("MERCHANDISER"));
                    List users = sqlCodeService.select("SEL_USER_BY_NAME", "TD_M_USER", getUserParams);
                    if (users.size() > 0){
                        Map user = (Map) users.get(0);
                        String email = user.get("EMAIL").toString();
                        mailParam.put("mailAddress", email);
                    }

                    mailParam.put("title", "订单审核通过通知");
                    needSendEmail.add(mailParam);
                }
            }
        }

        logParams.put("OPERATE_CODE", state);
        logParams.put("OPERATE_STAFF_ID", userName);
        logParams.put("REMARK", result.get("reason"));

        if (!intLogSQL.toString().endsWith(",")){
            return Utils.returnOkResult(context, "审核成功！");
        }

        String intSql = intLogSQL.substring(0, intLogSQL.length()-1);
        logger.debug("intLogSQL:{}", intSql);
        logger.debug("logParams:{}", logParams);

        //更新订单状态
        sqlParams.put("ORDER_IDS", waitAudit);
        sqlParams.put("STATE", state);
        sqlCodeService.update("UPD_ORDER_AUDIT", "TF_B_ORDER", sqlParams);

        //插入订单操作记录
        sqlCodeService.insert(intSql, logParams);

        //发送邮件
        if (needSendEmail.size() > 0){
            MailService mailService = (MailService) ToolFactory.getInstance().getContext().getBean("mailService");
            Map mailParams = new HashMap();
            mailParams.put("mailList", needSendEmail);
            mailService.sendMail(mailParams);
        }
        return Utils.returnOkResult(context, "订单审核成功！");
    }

    /**
     * 获取设计稿和唛头.
     * @param param
     * @throws Exception
     */
    private List getDesAndMark(Map param) throws Exception{

        logger.debug("********* getDesAndMark begin, param={} *********", param);

        List<Map> list = Lists.newArrayList();

        list = ToolFactory.getInstance().getSqlCodeService()
                .select("SEL_ORDER_ATTACH", "TF_B_ORDER_ATTACH", param);

        logger.debug("********* getDesAndMark end, list={} *********", list);

        return list;
    }

    /**
     * 确认生产
     * @param context
     * @return
     * @throws Exception
     */
    public Boolean product(Context context)throws Exception{
        Map order = (Map) context.get("order");
        String userName = context.get("userName").toString();
        SqlCodeService sqlCodeService = ToolFactory.getInstance().getSqlCodeService();
        Map sqlParams = new HashMap();
        sqlParams.put("ORDER_ID", order.get("ORDER_ID"));

        List orderFiles = sqlCodeService.select("SEL_ORDER_FILE", "TF_B_ORDER_ATTACH", sqlParams);

        HashSet orderF = new HashSet();
        for (int i = 0; i < orderFiles.size(); i++) {
            Map file = (Map) orderFiles.get(i);
            orderF.add(file.get("CATEGORY_CODE"));
        }

        //拖鞋
        List<Map<String,Object>> orderSlippers = sqlCodeService.select("SEL_SLIPPER_DETAIL","TF_B_ORDER_SLIPPER",sqlParams);
        for (int i = 0; i < orderSlippers.size(); i++) {
            Map slipper = orderSlippers.get(i);
            if (slipper.get("UNIT_PRICE") == null
                    || !orderF.contains("0")){
                throw new BusiException(ErrorCode.PRODUCTING_ORDER_PARAMS_ERROR, "数据不完整，请核实！");
            }
        }

        //香皂
        List<Map<String,Object>> orderSoaps = sqlCodeService.select("SEL_SOAP_DETAIL","TF_B_ORDER_SOAP",sqlParams);
        for (int i = 0; i < orderSoaps.size(); i++) {
            Map soaps = orderSoaps.get(i);
            if (soaps.get("UNIT_PRICE") == null
                    || soaps.get("PACKING_PRICE") == null
                    || !orderF.contains("3")){
                throw new BusiException(ErrorCode.PRODUCTING_ORDER_PARAMS_ERROR, "数据不完整，请核实！");
            }
        }

        //软管
        List<Map<String,Object>> orderHoses = sqlCodeService.select("SEL_HOSEWASH_DETAIL","TF_B_ORDER_HOSEWASH",sqlParams);
        for (int i = 0; i < orderHoses.size(); i++) {
            Map hoses = orderHoses.get(i);
            if (hoses.get("UNIT_PRICE") == null
                    || hoses.get("LIQUID_PACKING_PRICE") == null
                    || !orderF.contains("1")){
                throw new BusiException(ErrorCode.PRODUCTING_ORDER_PARAMS_ERROR, "数据不完整，请核实！");
            }
        }

        //瓶装
        List<Map<String,Object>> orderBottled = sqlCodeService.select("SEL_BOTTLEDWASH_DETAIL","TF_B_ORDER_BOTTLEDWASH",sqlParams);
        for (int i = 0; i < orderBottled.size(); i++) {
            Map bottled = orderBottled.get(i);
            if (bottled.get("UNIT_PRICE") == null
                    || bottled.get("LIQUID_PACKING_PRICE") == null
                    || !orderF.contains("2")){
                throw new BusiException(ErrorCode.PRODUCTING_ORDER_PARAMS_ERROR, "数据不完整，请核实！");
            }
        }

        //包装干货
        List<Map<String,Object>> orderDries = sqlCodeService.select("SEL_DRY_DETAIL","TF_B_ORDER_DRY",sqlParams);
        for (int i = 0; i < orderDries.size(); i++) {
            Map dries = orderDries.get(i);
            if (dries.get("IN_PACKING_PRICE") == null
                    || dries.get("OUT_PACKING_PRICE") == null
                    || dries.get("PACKING_TIME") == null
                    || !orderF.contains("4")){
                throw new BusiException(ErrorCode.PRODUCTING_ORDER_PARAMS_ERROR, "数据不完整，请核实！");
            }
        }


        List<Map<String,Object>> orderCustoms = sqlCodeService.select("SEL_CUSTOM_DETAIL","TF_B_ORDER_CUSTOM",sqlParams);
        for (int i = 0; i < orderCustoms.size(); i++) {
            Map customs = orderCustoms.get(i);
            if (customs.get("UNIT_PRICE") == null
                    || !orderF.contains("5")){
                throw new BusiException(ErrorCode.PRODUCTING_ORDER_PARAMS_ERROR, "数据不完整，请核实！");
            }
        }

        //更新订单状态
        sqlParams.put("ORDER_ID", order.get("ORDER_ID"));
        sqlParams.put("USER_NAME", userName);
        sqlCodeService.update("UPD_ORDER_PRODUCT", "TF_B_ORDER", sqlParams);

        //记录变更记录
        sqlParams.put("OPERATE_CODE", "4");
        sqlParams.put("OPERATE_STAFF_ID", userName);
        sqlParams.put("REMARK", "确认生产");
        sqlCodeService.insert("INT_COMM", "TF_B_ORDER_LOG", sqlParams);

        return Utils.returnOkResult(context, "确认生产成功！");
    }

    /**
    * 获取订单操作记录
    * @param context
    * @return
    * @throws Exception
    */
    public Boolean getLogsByOrderId(Context context) throws Exception{
        String orderId = (String) context.get("orderId");
        Map<String,String> params = Maps.newHashMap();

        params.put("ORDER_ID",orderId);
        SqlCodeService sqlCodeService = ToolFactory.getInstance().getSqlCodeService();
        List list = sqlCodeService.select("SEL_ORDER_LOG","TF_B_ORDER_LOG",params);

        return Utils.returnOk(context,list);
    }

    public Boolean auditCheck(Context context) throws Exception{

        Map result = Maps.newHashMap();
        List orders = (List) context.get("orders");
        if (orders.size() < 1){
            logger.error("orders is null");
            return Utils.returnFalseResult(context, ErrorCode.ORDER_IDS_IS_UNLL, "订单编号错误");
        }
        Boolean markFlag = false;
        Map checkRs = Maps.newHashMap();
        Boolean isBreak = false;
        for (int i = 0; i < orders.size(); i++) {
            Map map = (Map) orders.get(i);

            List dmList = getDesAndMark(map);

            if (dmList.size() < 1) {
                logger.error("getDesAndMark is null");
                return Utils.returnFalseResult(context, ErrorCode.ORDER_MARK_IS_NULL, "唛头不能为空，请上传");
            }

            List tabOrder = ToolFactory.getInstance().getSqlCodeService()
                    .select("SEL_TAB_ORDER", "TF_B_TAB_ORDER", map);
            if (tabOrder.size() < 1){
                logger.error("No tab order exist in tb_b_tab_order, param = {}", map);
                return Utils.returnFalseResult(context, ErrorCode.ORDER_TAB_ORDER_IS_NULL, "不存在商品顺序");
            }

            for (int j = 0; j < tabOrder.size(); j++){
                Map tmp = (Map) tabOrder.get(j);
                String tabType = StringUtils.isEmpty(tmp.get("TAB_ORDER"))? "" : tmp.get("TAB_ORDER").toString();
                String[] tabArr = tabType.split(",");
                checkRs = doCheck(tabArr, dmList, map, result);
                if (!checkRs.get("RESLUT_CODE").equals("0000")){
                    isBreak = true;
                    break;
                }
            }
            if (isBreak){
                break;
            }

        }

        if (isBreak){
            logger.debug("rsCode = {}, mag = {}", checkRs.get("RESLUT_CODE"), checkRs.get("RESLUT_MSG"));
            return Utils.returnFalseResult(context, checkRs.get("RESLUT_CODE").toString(), checkRs
                    .get("RESLUT_MSG").toString());
        }

        return Utils.returnOkResult(context, "校验通过");
    }

    /**
     * 订单ID校验.
     *
     * @param context
     * @return
     */
    public Boolean checkOrderId(Context context) throws Exception{

        logger.debug("*************** checkOrderId begin, context = {} ***************", context);

        Map param = Maps.newHashMap();
        param.put("ORDER_ID", context.get("ORDER_ID"));

        List list = ToolFactory.getInstance().getSqlCodeService()
                .select("SEL_ORDER_BY_ID", "TF_B_ORDER", param);

        if (list.size() > 0) {
            logger.debug("*************** checkOrderId end ***************", context);
            logger.error("ORDER_ID = {} has already exist in TF_B_ORDER", context.get("ORDER_ID"));
            return Utils.returnFalseResult(context, ErrorCode.ORDER_ID_EXIST, "订单编号已存在请重新输入");
        }

        logger.debug("*************** checkOrderId end, context = {} ***************", context);
        return Utils.returnOkResult(context, "操作完成");
    }

    private Map nullCheck(Map map, String str){
        Map rsM = Maps.newHashMap();
        rsM.put("FLAG", true);
        String[] keys = str.split(",");
        for (int i = 0; i < keys.length; i++){
            String[] kv = keys[i].split("\\*");
            if (map.containsKey(kv[0]) && StringUtils.isEmpty(map.get(kv[0]))){
                logger.debug("UNIT_PRICE IS NULL");
                rsM.put("FLAG", false);
                rsM.put("MSG", "字段" + kv[1] + "不能为空");
                rsM.put("IDX", i+1);
                break;
            }
        }
        return rsM;
    }

    private boolean maskCheck(List list, String codeType){
        Boolean markFlag = false;
        for (int m = 0; m < list.size(); m++){
            Map dm = (Map) list.get(m);
            if (dm.get("CATEGORY_CODE").toString().equals(codeType) && dm.get("UPLOAD_TYPE").toString().equals("1")){
                markFlag = true;
                break;
            }
        }
        return markFlag;
    }

    private void dataCheck(List list, Map result, String filds, String cateType){
        for (int j = 0; j < list.size(); j++){
            Map tmp = (Map) list.get(j);
            Map r = nullCheck(tmp, filds);
            if (!Boolean.valueOf(r.get("FLAG").toString())){
                result.put("MSG", cateType + "第" + (j+1) + "记录，" + r.get("MSG"));
                break;
            }
        }
    }

    private Map doCheck(String[] tabOrderArr, List dmList, Map map, Map result) throws Exception{
        Map checkRs = Maps.newHashMap();
        checkRs.put("RESLUT_CODE", "0000");
        boolean isBreak = false;
        for (int i = 0; i < tabOrderArr.length; i++){

            String type = tabOrderArr[i];
            switch (type){
                case "SLIPPER_INFO":{
                    //拖鞋
                    List slipperList = ToolFactory.getInstance().getSqlCodeService()
                            .select("SEL_SLIPPER_DETAIL", "TF_B_ORDER_SLIPPER", map);
                    if (slipperList.size() > 0) {

                        if (!maskCheck(dmList, "0")){
                            checkRs.put("RESLUT_CODE", ErrorCode.ORDER_AUDIT_CHECK_SLIPPER_ERROR);
                            checkRs.put("RESLUT_MSG", "拖鞋唛头不能为空，请上传");
                            isBreak = true;
                            break;
                        }

                        dataCheck(slipperList, result, "UNIT_PRICE*单价", "拖鞋");
                        if (result.size() > 0){
                            checkRs.put("RESLUT_CODE", ErrorCode.ORDER_AUDIT_CHECK_SLIPPER_ERROR);
                            checkRs.put("RESLUT_MSG", result.get("MSG").toString());
                            isBreak = true;
                            break;
                        }
                    }
                    break;
                }
                case "SOAP_INFO":{

                    //香皂
                    List soapList = ToolFactory.getInstance().getSqlCodeService()
                            .select("SEL_SOAP_DETAIL", "TF_B_ORDER_SOAP", map);
                    if (soapList.size() > 0) {

                        if (!maskCheck(dmList, "3")){
                            checkRs.put("RESLUT_CODE", ErrorCode.ORDER_AUDIT_CHECK_SOAP_ERROR);
                            checkRs.put("RESLUT_MSG", "香皂唛头不能为空，请上传");
                            isBreak = true;
                            break;
                        }

                        dataCheck(soapList, result, "UNIT_PRICE*皂体单价,PACKING_PRICE*包材加价", "香皂");
                        if (result.size() > 0){
                            checkRs.put("RESLUT_CODE", ErrorCode.ORDER_AUDIT_CHECK_SOAP_ERROR);
                            checkRs.put("RESLUT_MSG", result.get("MSG").toString());
                            isBreak = true;
                            break;
                        }

                    }

                    break;
                }
                case "HOSEWASH_INFO":{

                    //软管洗护
                    List hoseWashList = ToolFactory.getInstance().getSqlCodeService()
                            .select("SEL_HOSEWASH_DETAIL", "TF_B_ORDER_HOSEWASH", map);
                    if (hoseWashList.size() > 0) {
                        if (!maskCheck(dmList, "1")){
                            checkRs.put("RESLUT_CODE", ErrorCode.ORDER_AUDIT_CHECK_HOSEWASH_ERROR);
                            checkRs.put("RESLUT_MSG", "软管洗护唛头不能为空，请上传");
                            isBreak = true;
                            break;
                        }

                        dataCheck(hoseWashList, result, "UNIT_PRICE*软管单价,LIQUID_PACKING_PRICE*液体罐装费", "软管洗护");
                        if (result.size() > 0){
                            checkRs.put("RESLUT_CODE", ErrorCode.ORDER_AUDIT_CHECK_HOSEWASH_ERROR);
                            checkRs.put("RESLUT_MSG", result.get("MSG").toString());
                            isBreak = true;
                            break;
                        }

                    }

                    break;
                }
                case "CUSTOM_INFO":{

                    //自定义
                    List customList = ToolFactory.getInstance().getSqlCodeService()
                            .select("SEL_CUSTOM_DETAIL", "TF_B_ORDER_CUSTOM", map);
                    if (customList.size() > 0) {
                        if (!maskCheck(dmList, "5")){
                            checkRs.put("RESLUT_CODE", ErrorCode.ORDER_AUDIT_CHECK_CUSTOM_ERROR);
                            checkRs.put("RESLUT_MSG", "自定义唛头不能为空，请上传");
                            isBreak = true;
                            break;
                        }

                        dataCheck(customList, result, "UNIT_PRICE*单价", "自定义");
                        if (result.size() > 0){
                            checkRs.put("RESLUT_CODE", ErrorCode.ORDER_AUDIT_CHECK_CUSTOM_ERROR);
                            checkRs.put("RESLUT_MSG", result.get("MSG").toString());
                            isBreak = true;
                            break;
                        }

                    }

                    break;
                }
                case "BOTTLEDWASH_INFO":{

                    //瓶装洗护
                    List bottledWashList = ToolFactory.getInstance().getSqlCodeService()
                            .select("SEL_BOTTLEDWASH_DETAIL", "TF_B_ORDER_BOTTLEDWASH", map);
                    if (bottledWashList.size() > 0) {

                        if (!maskCheck(dmList, "2")){
                            checkRs.put("RESLUT_CODE", ErrorCode.ORDER_AUDIT_CHECK_BOTTLEDWASH_ERROR);
                            checkRs.put("RESLUT_MSG", "瓶装洗护唛头不能为空，请上传");
                            isBreak = true;
                            break;
                        }

                        dataCheck(bottledWashList, result, "UNIT_PRICE*瓶子单价,LIQUID_PACKING_PRICE*液体罐装费", "瓶装洗护");
                        if (result.size() > 0){
                            checkRs.put("RESLUT_CODE", ErrorCode.ORDER_AUDIT_CHECK_BOTTLEDWASH_ERROR);
                            checkRs.put("RESLUT_MSG", result.get("MSG").toString());
                            isBreak = true;
                            break;
                        }

                    }

                    break;
                }
                case "DRY_INFO":{

                    //包装干货
                    List dryList = ToolFactory.getInstance().getSqlCodeService()
                            .select("SEL_DRY_DETAIL", "TF_B_ORDER_DRY", map);
                    if (dryList.size() > 0) {
                        if (!maskCheck(dmList, "4")){
                            checkRs.put("RESLUT_CODE", ErrorCode.ORDER_AUDIT_CHECK_DRY_ERROR);
                            checkRs.put("RESLUT_MSG", "包装干货唛头不能为空，请上传");
                            isBreak = true;
                            break;
                        }

                        dataCheck(dryList, result, "IN_PACKING_PRICE*内包材单价,OUT_PACKING_PRICE*外包材单价,PACKING_TIME*包工", "包装干货");
                        if (result.size() > 0){
                            checkRs.put("RESLUT_CODE", ErrorCode.ORDER_AUDIT_CHECK_DRY_ERROR);
                            checkRs.put("RESLUT_MSG", result.get("MSG").toString());
                            isBreak = true;
                            break;
                        }

                    }

                    break;
                }
            }
            if (isBreak){
                break;
            }
        }

        return checkRs;
    }


}
